package com.orangelife.mobile.goodsrent.adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.util.DecimalUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.goodsrent.biz.GoodsInfoCallBack;
import com.orangelife.mobile.goodsrent.dao.GoodsDao;
import com.orangelife.mobile.goodsrent.entity.GoodsInfo;
import com.orangelife.mobile.util.ToastHelper;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2015/12/16.
 */
public class GoodsRentDayAdapter extends FatherAdapter<GoodsInfo> {
    private GoodsInfoCallBack goodsInfoCallBack;
    private List<GoodsInfo> mGoodsInfoList;
    public GoodsRentDayAdapter(List goodsInfoList,GoodsInfoCallBack goodsInfoCallBack) {
        this.mGoodsInfoList = goodsInfoList;
        this.goodsInfoCallBack = goodsInfoCallBack;
    }

    @Override
    public BaseAdapter setList(List<GoodsInfo> list) {
        this.mGoodsInfoList = list;
        return GoodsRentDayAdapter.this;
    }

    @Override
    public int getCount() {
        return mGoodsInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return mGoodsInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            convertView  = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.item_goods_rent, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        GoodsInfo goodsInfo = mGoodsInfoList.get(position);
        MyOnClicker myOnClicker = new MyOnClicker(goodsInfo);
        holder.tvRoodsName.setText(goodsInfo.getGoodName());
        holder.tvRentDay.setText(goodsInfo.getDay()+"天");
        holder.ibMinusDay.setOnClickListener(myOnClicker);
        holder.ibAddDay.setOnClickListener(myOnClicker);
        return convertView;
    }

    class ViewHolder {
        /**租借物品名称*/
        @Bind(R.id.tvGoodsName)
        TextView tvRoodsName;
        /**天数*/
        @Bind(R.id.tvRentDay)
        TextView tvRentDay;
        /**减少天数*/
        @Bind(R.id.ibMinusDay)
        ImageButton ibMinusDay;
        /**增加天数*/
        @Bind(R.id.ibAddDay)
        ImageButton ibAddDay;
        public ViewHolder(View v) {
            ButterKnife.bind(this, v);
        }
    }

    class MyOnClicker implements View.OnClickListener {
        private GoodsInfo goodsInfo;
        private String day;
        public MyOnClicker(GoodsInfo goodsInfo){
            this.goodsInfo = goodsInfo;
            day = goodsInfo.getDay();
        }
        @Override
        public void onClick(View view) {
           switch (view.getId()){
               case R.id.ibMinusDay:
                   day = DecimalUtil.subtract(day,"1");
                   if(Double.parseDouble(day)>0){
                       String updatePrice = DecimalUtil.subtract(goodsInfo.getUnitPrice(),goodsInfo.getPrice());
                       GoodsDao.getInstance().setRentDay(goodsInfo, day, updatePrice);
                   }else{
                       GoodsDao.getInstance().deleteGoodsInfo(goodsInfo);
                   }
                   break;
               case R.id.ibAddDay:
                     day = DecimalUtil.add(day, "1");
                   if(Double.parseDouble(day)<=Double.parseDouble(goodsInfo.getPdtDesc())){
                       /**更新天数和总价*/
                       String updatePrice = DecimalUtil.add(goodsInfo.getUnitPrice(),goodsInfo.getPrice());
                       GoodsDao.getInstance().setRentDay(goodsInfo, day,updatePrice);
                   }else{
                       ToastHelper.getInstance()._toast("超过可租借天数");
                   }
                   break;

           }
            /**更新adapter*/
            setList(GoodsDao.getInstance().selectUserInfoList());
            notifyDataSetChanged();
            /**更新购物车数量提示*/
            goodsInfoCallBack.getGoodsRentNum(GoodsDao.getInstance().getGoodsNum());
            /**更新物品总价*/
            goodsInfoCallBack.getGoodsTotalPrice(GoodsDao.getInstance().getGoodsTotalPrice());
        }
    }
}
